//
//  CGQxiugaimimaViewController.swift
//  SheYangBigData
//
//  Created by 叁拾叁 on 2021/4/20.
//  Copyright © 2021 叁拾叁. All rights reserved.
//



import UIKit
import MJRefresh
import RxSwift
import RxDataSources
import Then
import SDCycleScrollView

class CGQxiugaimimaViewController: SYBaseTableViewController {
    
    fileprivate var vm: VGTableViewPageViewModel<CGQyuweiModel>!
    var params = [String: String]()

    var delete: UIButton = {
        let v = UIButton()
        v.setTitle("退出", for: .normal)
        v.setTitleColor(UIColor(hex: 0x0782FF), for: .normal)
        v.titleLabel?.font = 18.autoFontSize()
        v.layer.cornerRadius = 3
        v.layer.masksToBounds = true
        v.backgroundColor = UIColor.white
        return v
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setTableView()
        setNoDataView()

        self.loadDataSuccessed()
        
        self.view.backgroundColor = UIColor(hex: 0xF7F7F7)
        self.title = "修改密码"
    }
    
    
    
    @objc func rightAction() {
        
    }
    
    func loadDataSuccessed() {
        DispatchQueue.main.async {
            
            self.setDataSource()
        }
    }
    
    func setTableView() {
        
        //创建表格视图
        self.tableView = UITableView(frame: CGRect(x: 0, y: NAV_HEIGHT, width: SCREEN_WIDTH, height: 380.autoWidth()), style:.plain)
        self.tableView.separatorStyle = .none
        self.tableView.backgroundColor = .clear
        self.tableView!.register(UITableViewCell.self, forCellReuseIdentifier: "Cell")
        self.tableView.isScrollEnabled = false
        self.view.addSubview(self.tableView!)
        
        tableView.rx.itemSelected
            .subscribe(onNext:{ [weak self] indexPath in
                if let `self` = self {
                    
                }
            })
            .disposed(by:dispose)
        
        delete.frame = CGRect(x: 10.autoWidth(), y: NAV_HEIGHT+380.autoWidth(), width: SCREEN_WIDTH - 20.autoWidth(), height: 44.autoWidth())
        delete.backgroundColor = UIColor(hex: 0x0782FF)
        delete.setTitle("保存", for: .normal)
        delete.setTitleColor(.white, for: .normal)
        delete.titleLabel?.font = 18.autoFontSize()
        self.view.addSubview(delete)
        
        delete.addAction {[weak self] in
            if let `self` = self {
                
                if self.params["newPassword"] != self.params["password"] {
                    
                    self.sy_toast("请确认新密码与新密码一致")
                    
                    return
                }
                
                if self.params.values.count == 0  {
                    
                    self.sy_toast("请输入密码")
                    return
                }
                
                self.params.removeValue(forKey: "newPassword")
                self.requestVM.baseRequest(disposeBag: self.dispose, type: .xiugaimima(dic: self.params), modelClass: SGBaseNoPageListModel<NSMessageDataListModel>.self) {[weak self] (res) in
                    
                    if let `self` = self  {

                        self.clearAllUserDefaultsData()
                        SYTokenCheck.toLoginVC()
                    }
                    
                } Error: {
                    
                }
           
                
//                self.clearAllUserDefaultsData()
//                SYTokenCheck.toLoginVC()

            }
        }
    }
    
    
    /** 清除userDefaults所有数据*/
     
    func clearAllUserDefaultsData(){
       let userDefaults = UserDefaults.standard
       let dics = userDefaults.dictionaryRepresentation()
       for key in dics {
           userDefaults.removeObject(forKey: key.key)
       }
       userDefaults.synchronize()
    }
    func setNoDataView() {
        noDataView = SYNoDataView(frame: CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: SCREEN_HEIGHT-NAV_HEIGHT))
        noDataView.isHidden = true
        tableView.addSubview(noDataView)
    }
    
    func setDataSource() {
        //初始化数据
        let sections = Observable.just([
            CGQyuweiModelSection(header: "", items: [CGQyuweiModel(),CGQyuweiModel(),CGQyuweiModel()])//self.vm.dataSource)
        ])
        
        //创建数据源
        let dataSource = RxTableViewSectionedAnimatedDataSource<CGQyuweiModelSection>(
            //设置单元格
            configureCell: { ds, tv, ip, item in
                let cell: CGQxiugaimimaTableViewCell = tv.dequeueReusableCell(withIdentifier: "Cell")
                    as? CGQxiugaimimaTableViewCell ?? CGQxiugaimimaTableViewCell(style: .default, reuseIdentifier: "Cell")
                                
                cell.configureView(row: ip.row)
                
                cell.passParamsClosure = {[weak self] value in
                    if let `self` = self {
                        let v = value as! String

                        if ip.row == 0 {
                            
                            self.params["oldPassword"] =  v
                        
                        }else if ip.row == 1{
                            self.params["newPassword"] = v
                        }
                        if ip.row == 2 {
                            
                            self.params["password"] = v
                        }
                        
                        
                    }
                    
                }
                
                cell.selectionStyle = .none
                return cell
                
            },
            //设置分区头标题
            titleForHeaderInSection: { ds, index in
                return ds.sectionModels[index].header
            })
        tableView.dataSource = nil
        //绑定单元格数据
        sections
            .bind(to: tableView.rx.items(dataSource: dataSource))
            .disposed(by: dispose)
        
    }
    
    
}


//自定义Section
//struct CGQyuweiModelSection {
//    var header: String
//    var items: [Item]
//}
//
//extension CGQyuweiModelSection : AnimatableSectionModelType {
//    typealias Item = CGQyuweiModel
//
//    var identity: String {
//        return header
//    }
//
//    init(original: CGQyuweiModelSection, items: [Item]) {
//        self = original
//        self.items = items
//    }
//}
//
//extension CGQyuweiModel: IdentifiableType {
//    var identity: String {
//        return id!
//    }
//
//    typealias Identity = String
//}



